package cn.jdy.plugins.service;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.jdy.framework.db.hibernate.BaseDao;
import cn.jdy.framework.util.StringUtil;
import cn.jdy.platform.entity.ConfValue;
import cn.jdy.platform.service.ConfService;
import cn.jdy.plugins.entity.Product;
import cn.jdy.plugins.entity.AccessDetails;

/**
 * @title 商家管理后台操作
 * @author jga
 * @since 1-12-2015
 */
@Service
public class AccessDetailsService extends BaseDao<AccessDetails>{

	@Autowired
	private ConfService confService;
	
	/**
	 * 按条件查询
	 * @param sp
	 * 
	 * @return
	 */
	public List<AccessDetails> query(AccessDetails sp){
		if(sp==null) return super.findAll();
		if(sp.getiDisplayLength() > 0){
	     return super.find(createFilter(sp),sp.getiDisplayStart(),sp.getiDisplayLength());
			}
		return super.find(createFilter(sp));
	}
	 public List<AccessDetails> querypage(AccessDetails sp){
	       return super.find(createFilter(sp),sp.getPageNo(),18);
	  }
	public Long count(AccessDetails sp){
		return super.count(createFilter(sp));
	}
	/**
	 * 拼装条件
	 * @param sp
	 * 
	 * @return
	 */
	private DetachedCriteria createFilter(AccessDetails sp){
		DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
		if(!StringUtil.isNullOrEmpty(sp.getName())) {
	        dc.add(Restrictions.like("name", sp.getName(),MatchMode.ANYWHERE));
		} 
	
	
	  
    if(!StringUtil.isNullOrEmpty(sp.getCreateTime())&&!",".equals(sp.getCreateTime())) {
   
        if( sp.getCreateTime().split(",").length>1){
          dc.add(Restrictions.between("createTime", sp.getCreateTime().split(",")[0], sp.getCreateTime().split(",")[1]));
        }else{
          dc.add(Restrictions.like("createTime", sp.getCreateTime(),MatchMode.ANYWHERE));
        }
     
    }
	  


		dc.addOrder(Order.desc("createTime"));
			return dc;
	}
	/**
	 * 删除
	 * @param sp:一个accessDetails对象

	@Transactional
    public void delaccessDetails(accessDetails sp){
		StringBuilder sql = new StringBuilder("delete ");
	    sql.append(entityClass.getSimpleName());
	    sql.append(" where ");
	        sql.append("and id").append("=").append(sp.getId());
	        hibernateTemplate.bulkUpdate(sql.toString(), null);
	}   */
  public List<AccessDetails> findbytype(int type, String addres, int i, int limitMax) {
    DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
    dc.add(Restrictions.eq("status", "1"));
        if(null != addres){
          dc.add(Restrictions.eq("address", addres));
        }
      if(type==0){// 根据类排序，0 表示目前根据创建时间排序 ， 以后可能会根据推广，销售量等等排序
        dc.addOrder(Order.desc("createTime"));
      }else{
        //以后会有字段或者方案
      }
      
      return super.find(dc,i,limitMax);
  }

}
